package com.sxbbc.seller.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.RestResponse;
import com.sxbbc.common.core.entity.jz.BAccount;
import com.sxbbc.common.core.entity.jz.BMoneyDetail;
import com.sxbbc.common.core.entity.jz.BankDetail;
import com.sxbbc.common.core.entity.jz.BankType;
import com.sxbbc.common.core.entity.jz.SysCashDetails;
import com.sxbbc.common.core.enums.CashTypeEnums;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.mapper.jz.BAccountMapper;
import com.sxbbc.common.core.service.IBMoneyDetailService;
import com.sxbbc.common.core.service.IBankDetailService;
import com.sxbbc.common.core.service.IBankTypeService;
import com.sxbbc.common.core.service.ISysCashDetailsService;
import com.sxbbc.common.core.service.ISysCashRuleService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.seller.common.AccountMapping;
import com.sxbbc.seller.service.BusinessService;
import com.sxbbc.seller.util.BusinessSession;

/**
 * 店铺资产管理  控制层
 * @author xiaoky
 * @date 2019-06-04
 */
@Controller
public class AccountController {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private IBMoneyDetailService bMoneyDetailService;
	@Autowired
	private IBankDetailService bankDetailService;
	@Autowired   
	private IBankTypeService bankTypeService;
	@Autowired
	private BAccountMapper bAccountMapper;
	@Autowired
	private ISysCashRuleService sysCashRuleService;
	@Autowired
	private BusinessService businessService;
	@Autowired
	private ISysCashDetailsService sysCashDetailService;
	
	/**   
	 * 查询店铺金额明细
	 * @param page 分页数据
	 * @param moneyDetail 查询条件
	 * @return ModelAndView
	 */
	@RequestMapping(value = AccountMapping.SHOW_MONEY_DETAIL)
	public ModelAndView showMoneyDetail (HttpServletRequest request,BasePage<BMoneyDetail> page, BMoneyDetail moneyDetail) {
		if (StringUtils.isNotBlank(moneyDetail.getQueryTime())) {
            String[] times = moneyDetail.getQueryTime().split("~");
            moneyDetail.setStart(times[0].trim());
            moneyDetail.setEnd(times[1].trim());
        }
		Integer bid = BusinessSession.getBid(request);
		moneyDetail.setBid(bid);
		ModelAndView view = new ModelAndView(AccountMapping.SHOW_MONEY_DETAIL);
		IPage<BMoneyDetail> pageInfo = bMoneyDetailService.queryBusinessMoneyDetail(page, moneyDetail);
		List<HandleTypeEnums> handleList = HandleTypeEnums.queryList();
		view.addObject("pageInfo", pageInfo);
		view.addObject("details", moneyDetail);
		view.addObject("handleList", handleList);
		return view;
	}	
	
	/**
	 * 查询店铺银行卡
	 * @param page 分页
	 * @param bankDetail 查询条件
	 * @return 分页数据
	 */
	@RequestMapping(value = AccountMapping.SHOW_BUSINESS_BANK)
	public ModelAndView showBusinessBank (BasePage<BankDetail> page, BankDetail bankDetail) {
		Integer bid = BusinessSession.getBid(request);
		bankDetail.setTargetId(bid);
		bankDetail.setType(CashTypeEnums.BUSINESS.getId());
		QueryWrapper<BankDetail> wrapper = bankDetailService.queryWrapper(bankDetail);
		IPage<BankDetail> pageInfo = bankDetailService.page(page, wrapper);
		// 银行卡类型列表
		List<BankType> bankType = bankTypeService.list();
		ModelAndView view = new ModelAndView(AccountMapping.SHOW_BUSINESS_BANK);
		view.addObject("pageInfo", pageInfo);
		view.addObject("bankType", bankType);
		view.addObject("bankDetail", bankDetail);
		return view;
	}
	
	/**
	 * 删除店铺银行卡
	 * @param bankDetailId 银行卡id
	 * @return
	 */
	@RequestMapping(value = AccountMapping.DELETE_BANK_DETAIL)
	@ResponseBody
	public RestResponse deleteBankDetail (Integer bankDetailId) {
		Integer bid = BusinessSession.getBid(request);
		RestResponse result = bankDetailService.detailBankDetail(bid, bankDetailId, CashTypeEnums.BUSINESS.getId());
		return result;
	}
	
	/**
	 * 银行卡新增/编辑页面
	 * @param bankDetailId 银行卡id
	 * @return ModelAndView
	 */
	@RequestMapping(value = AccountMapping.SHOW_EDIT_BANK)
	public ModelAndView showEditBank (Integer bankDetailId) {
		BankDetail bankDetail = bankDetailService.getById(bankDetailId);
		ModelAndView view = new ModelAndView(AccountMapping.SHOW_EDIT_BANK);
		// 银行卡类型列表
		List<BankType> bankType = bankTypeService.list();
		view.addObject("bankType", bankType);
		view.addObject("bankDetail", bankDetail);
		return view;
	}
	
	/**
	 * 编辑银行卡
	 * @param detail
	 * @return 处理结果
	 */
	@RequestMapping(value = AccountMapping.EDIT_BANK_DETAIL)
	@ResponseBody
	public RestResponse editBankDetail (BankDetail detail) {
		Integer bid = BusinessSession.getBid(request);
		detail.setTargetId(bid);
		detail.setType(CashTypeEnums.BUSINESS.getId());
		return bankDetailService.editBankDetail(detail);
	}
	
	/**
	 * 商家提现页面
	 * @return ModelAndView
	 */
	@RequestMapping(value = AccountMapping.SHOW_BUSINESS_CASH)
	public ModelAndView showBusinessCash () {
		Integer bid = BusinessSession.getBid(request);
		ModelAndView view = new ModelAndView(AccountMapping.SHOW_BUSINESS_CASH);
		// 商家余额
		BAccount account = bAccountMapper.getByBid(bid);
		if (account != null) {
			view.addObject("money", account.getMoney());
		}
		// 商家银行卡
		List<BankDetail> bankDetails = bankDetailService.queryBankDetailList(bid, CashTypeEnums.BUSINESS.getId());
		view.addObject("bankDetails", bankDetails);
		// 提现规则
		Map<String, Object> rule = sysCashRuleService.getCashRule(CashTypeEnums.BUSINESS.getId());
		view.addObject("cashRule", rule);
		return view;
	}
	
	/**
	 * 处理店铺提现
	 * @param cashDetail
	 * @return
	 */
	@RequestMapping(value = AccountMapping.HANDLE_BUSINESS_CASH)
	@ResponseBody
	public RestResponse handleBusinessCash (SysCashDetails cashDetail) {
		Integer bid = BusinessSession.getBid(request);
		cashDetail.setTargetId(bid);
		cashDetail.setType(CashTypeEnums.BUSINESS.getId());
		return businessService.handleBusinessCashApply(cashDetail);
	}
	
	/**
	 * 店铺提现记录查询
	 * @param cashDetail 
	 * @return 
	 */
	@RequestMapping(value = AccountMapping.SHOW_CASH_DETAIL)
	public ModelAndView showCashDetail (BasePage<SysCashDetails> page, SysCashDetails cashDetail) {
		Integer bid = BusinessSession.getBid(request);
		cashDetail.setTargetId(bid);
		cashDetail.setType(CashTypeEnums.BUSINESS.getId());
		ModelAndView view = new ModelAndView(AccountMapping.SHOW_CASH_DETAIL);
		BasePage<SysCashDetails> pageInfo = sysCashDetailService.queryCashDetails(page, cashDetail);
		view.addObject("pageInfo", pageInfo);
		view.addObject("detail", cashDetail);
		return view;
	}
}
